package com.fau.fm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.fm.action.PublicAction;
import com.fau.fm.entity.MedicalAdmin;
import com.fau.fm.entity.MedicalOrderHasReconciliation;
import com.fau.fm.util.DateUtil;
import com.fau.fm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 对账订单Action
 * @author dangjh
 * @datetime 2014年11月24日,下午4:01:08
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class ReconciledAction extends PublicAction implements ModelDriven<MedicalOrderHasReconciliation> {

	private static final long serialVersionUID = -9023360900448288592L;

	private MedicalOrderHasReconciliation order = new MedicalOrderHasReconciliation();
	
	/**
	 * @desc 实例化已对账订单查询界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午4:00:22
	 */
	@Action(value = "all_audited_checking", results = {@Result(location = "all_audited_checking.jsp")})
	public String allAuditedChecking(){
		return SUCCESS;
	}
	
	/**
	 * @desc 实例化已对账订单查询(线下汇款)界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午4:00:22
	 */
	@Action(value = "order_remittance_inquiry_init", results = {@Result(name = SUCCESS, location = "order_remittance_inquiry_init.jsp")})
	public String orderRemittanceInquiryInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 实例化已对账订单查询(货到付款)界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午4:00:22
	 */
	@Action(value = "order_delivery_inquiry_init", results = {@Result(name = SUCCESS, location = "order_delivery_inquiry_init.jsp")})
	public String orderDeliveryInquiryInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 订单查询列表数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午4:08:48
	 */
	@Action(value = "order_inquiry_view")
	public String orderInquiryView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		order.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getStatisticsService().findAllOrderReconciliation(map, order);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 已对账订单信息导出
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:16:01
	 */
	@Action(value = "exports_orders_info")
	public String exports() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"已对账订单信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().selectedPage(order);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部已对账订单信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:17:06
	 */
	@Action(value = "exports_all_orders_info")
	public String exportsAll() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			order.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"已对账订单信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().allPage(order);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 实例化已对账订单统计界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午4:00:22
	 */
	@Action(value = "audited_checking_sta", results = {@Result(location = "audited_checking_sta.jsp")})
	public String allAuditedCheckingSta(){
		return SUCCESS;
	}
	
	/**
	 * @desc 订单统计列表数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午4:08:48
	 */
	@Action(value = "order_inquiry_sta_view")
	public String orderInquiryStaView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		order.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getStatisticsService().findAllOrderReconSta(map, order);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 已对账订单统计导出
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:16:01
	 */
	@Action(value = "exports_sta_info")
	public String exportsSta() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"已对账订单统计"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().selectedReconPage(order);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部已对账订单统计
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:17:06
	 */
	@Action(value = "exports_all_sta_info")
	public String exportsStaAll() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			order.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"已对账订单统计"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().allReconPage(order);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrderHasReconciliation getModel() {
		return order;
	}

}
